<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 注销账号 开发者: 麦沃德科技-半夏  
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}">
		<!-- 内容区 -->
		<view class="container-main" v-if="loadEnd">
			<view class="main-title">注销须知</view>
			<view class="main-content">
				<mp-html :content="richText"></mp-html>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-read flex align-items-center" @click="isAgree = !isAgree">
				<view class="read-bg"></view>
				<view class="read-radio" :style="{background: isAgree ?  themeColor : '#E1E1E1'}">
					<image class="radio-icon" src="/static/tick.png" mode="aspectFit" v-if="isAgree"></image>
				</view>
				<view class="read-label">
					阅读并同意<text class="text" @click.stop="toAgreement()">《注销协议》</text>
				</view>
			</view>
			<view class="footer-btn" @click="handConfirm()">确认注销</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 是否同意
				isAgree: false,
				// 注销须知
				richText: "",
				// 延时器
				timeout: null,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad() {
			this.getContent(() => {
				this.loadEnd = true
			})
		},
		onUnload() {
			clearTimeout(this.timeout)
		},
		methods: {
			// 获取注销须知
			getContent(fn) {
				this.$util.request("main.cancelAgreement").then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.richText = res.data.log_off_notice
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取注销须知 ', error)
				})
			},
			// 跳转注销协议
			toAgreement() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/driver/mine/settings/agreement?type=-1"
				})
			},
			// 确认注销
			handConfirm() {
				if (!this.isAgree) {
					uni.showToast({
						title: '请阅读并同意注销协议',
						icon: 'none',
					})
					return
				}
				uni.showModal({
					title: "注意",
					content: "账号注销后无法恢复，是否继续？",
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: "加载中",
								mask: true
							})
							this.$util.request("login.logoff").then(res => {
								uni.hideLoading()
								if (res.code == 1) {
									uni.showToast({
										title: '注销成功',
										icon: 'success',
										mask: true,
										duration: 1500
									})
									this.$store.commit('user/clearAuth')
									this.timeout = setTimeout(() => {
										if (getCurrentPages().length == 1) {
											this.$util.toPage({
												mode: 3,
												path: "/pages/driver/mine/index",
											})
										} else {
											uni.navigateBack()
										}
									}, 1500)
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							}).catch(error => {
								uni.hideLoading()
								console.error('注销账号', error)
							})
						}
					},
				})
			},
		},
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx 32rpx 180rpx;

		.container-main {
			padding: 32rpx;
			background: #FFF;
			border-radius: 32rpx;

			.main-title {
				color: #242629;
				font-size: 32rpx;
				font-weight: 600;
				line-height: 44rpx;
				text-align: center;
			}

			.main-content {
				margin-top: 24rpx;
				color: #242629;
				font-size: 28rpx;
				line-height: 50rpx;
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;
			padding-bottom: 16rpx;

			.footer-read {
				padding: 16rpx 32rpx;
				background: #FFF;
				position: relative;
				z-index: 1;

				.read-bg {
					position: absolute;
					top: 0;
					right: 0;
					bottom: 0;
					left: 0;
					z-index: -1;
					background: var(--theme-color);
					opacity: .05;
				}

				.read-radio {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					background: #E1E1E1;
				}

				.read-label {
					margin-left: 24rpx;
					color: #5A5B6E;
					text-align: center;
					font-size: 28rpx;
					line-height: 40rpx;

					.text {
						color: var(--theme-color);
					}
				}
			}

			.footer-btn {
				margin: 16rpx 24rpx 0;
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				background: var(--theme-color);
				text-align: center;
			}
		}
	}
</style>